//用户信息数据
function createUserList() {
  return [
    {
      userId: 1,
      avatar:
        "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
      username: "admin",
      password: "123456",
      desc: "超级管理员",
      roles: ["超级管理员"],
      buttons: ["cuser.details", "cuser.user"],
      routers: ["home"],
      token: "Admin Token",
    },
    {
      userId: 2,
      avatar:
        "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
      username: "张三",
      password: "123456",
      desc: "平台管理员",
      roles: ["平台管理员"],
      buttons: ["cuser.details"],
      routers: ["home"],
      token: "Admin Tokens",
    },
  ];
}

export default [
  //用户登录接口
  {
    url: "/api/user/login", //请求地址
    method: "post", //请求方式
    response: ({ body }) => {
      //获取请求体携带过来的用户名和密码
      const { username, password } = body;
      //调取用户信息函数，用于判断是否有此用户
      const checkUser = createUserList().find(
        (item) => item.username === username && item.password === password,
      );
      //没有用户返回失败信息
      if (!checkUser) {
        return { code: 201, data: { message: "账号或者密码不正确" } };
      }
      //如果有返回信息
      const { token } = checkUser;
      return { code: 200, data: { token } };
    },
  },
  //获取用户信息
  {
    url: "/api/user/info", //请求地址
    method: "get", //请求方式
    response: (request) => {
      //获取请求头携带token
      const token = request.headers.token;
      //查看用户信息是否包含有次token用户
      const checkUser = createUserList().find((item) => item.token === token);
      //没有用户返回失败信息
      if (!checkUser) {
        return { code: 201, data: { message: "获取用户信息失败" } };
      }
      //如果有返回信息
      return { code: 200, data: { checkUser: checkUser } };
    },
  },
];
